<?php
/* SVN FILE: $Id: taxes.php 191 2009-09-01 22:19:51Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Manage Forums
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 191 $
* @lastrevision $Date: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/manage/taxes.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_manage_taxes extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'manage_taxes' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_manage');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_manage' ) );
		
		$execute = array(
							'manage' => 'manageTaxes',
							'add' => 'addTax',
							'insert' => 'insertTax',
							'edit' => 'editTax',
							'update' => 'updateTax',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'manage';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function manageTaxes()
	{
		$this->registry->output->html = $this->html->manageTaxes();
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function addTax()
	{
		$tax = array();
		$tax['applying'] = "hand";
		$tax['t_week'] = 0;
		$tax['t_day'] = 0;
		$tax['t_hour'] = 0;
		$tax['t_minute'] = 0;
		$tax['tax_fix'] = 0;
		$tax['tax_percent'] = 0;
		$tax['tax_pmin'] = 0;
		$tax['tax_psub'] = 0;
		$tax['range_min'] = 0;
		$tax['range_max'] = 0;
		$this->registry->output->html = $this->html->taxForm($tax, 'insert', $this->lang->words['tax_add_title'], $this->lang->words['tax_add_button']);
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function insertTax()
	{
		if(!in_array($this->request['applying'], array('post', 'hand')))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=taxes&amp;do=add" );
		}
		
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$tax = array();
		$tax['applying'] = $this->request['applying'];
		$tax['t_week'] = min(max(intval($this->request['t_week']),0),4);
		$tax['t_day'] = min(max(intval($this->request['t_day']),0),6);
		$tax['t_hour'] = min(max(intval($this->request['t_hours']),0),23);
		$tax['t_minute'] = min(max(intval($this->request['t_minute']),0),59);
		$tax['tax_fix'] = numbers::parseInt($this->request['tax_fix']);
		$tax['tax_percent'] = numbers::parseFloat($this->request['tax_percent']);
		$tax['tax_pmin'] = numbers::parseInt($this->request['tax_pmin']);
		$tax['tax_psub'] = numbers::parseInt($this->request['tax_psub']);
		$tax['range_min'] = numbers::parseInt($this->request['range_min']);
		$tax['range_max'] = numbers::parseInt($this->request['range_max']);
		
		$this->DB->insert('market_taxes', $tax);
		
		$this->cache->rebuildCache('market_taxes','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['add_tax_log'] );
		
		$this->registry->output->global_message = $this->lang->words['tax_added'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=taxes" );
	}
	
	public function editTax()
	{
		if(!is_array($this->caches['market_taxes']['id'][$this->request['tax']]))
		{
			$this->registry->output->showError(null);
		}
		
		$this->registry->output->html = $this->html->taxForm($this->caches['market_taxes']['id'][$this->request['tax']], 'update', $this->lang->words['tax_edit_title'], $this->lang->words['tax_edit_button']);
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function updateTax()
	{
		if(!is_array($this->caches['market_taxes']['id'][$this->request['tax']]))
		{
			$this->registry->output->showError(null);
		}
		
		if(!in_array($this->request['applying'], array('post', 'hand')))
		{
			$this->registry->output->showError(null);
		}
		
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$tax = array();
		$tax['applying'] = $this->request['applying'];
		$tax['t_week'] = min(max(intval($this->request['t_week']),0),4);
		$tax['t_day'] = min(max(intval($this->request['t_day']),0),6);
		$tax['t_hour'] = min(max(intval($this->request['t_hours']),0),23);
		$tax['t_minute'] = min(max(intval($this->request['t_minute']),0),59);
		$tax['tax_fix'] = numbers::parseInt($this->request['tax_fix']);
		$tax['tax_percent'] = numbers::parseFloat($this->request['tax_percent']);
		$tax['tax_pmin'] = numbers::parseInt($this->request['tax_pmin']);
		$tax['tax_psub'] = numbers::parseInt($this->request['tax_psub']);
		$tax['range_min'] = numbers::parseInt($this->request['range_min']);
		$tax['range_max'] = numbers::parseInt($this->request['range_max']);
		
		$this->DB->update('market_taxes', $tax, "t_id=".$this->request['tax']);
		
		$this->cache->rebuildCache('market_taxes','ibmarket');
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['edit_tax_log'] );
		
		$this->registry->output->global_message = $this->lang->words['tax_edited'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=taxes" );
	}
}